<!--
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2024-10-11 13:13:36
 * @LastEditTime: 2024-11-01 15:54:24
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description: 
 * @FilePath: \markdown-magic\src\pages\MarkdownMagic\components\Menu\index.vue
 * Copyright 版权声明
-->
<template>
  <div class="menuWrapper">
    <a-dropdown v-for="(item, index) in menuOptions" :key="item.key">
      <a class="ant-dropdown-link" @click.prevent>
        {{item.label}}
      </a>
      <template #overlay>
        <a-menu>
          <template v-for="(items, indexs) in item.children">
            <a-menu-item @click="handleClick(items)">
              {{items.label}}
            </a-menu-item>
            <a-menu-divider v-if="items.divider === true"/>
          </template>
        </a-menu>
      </template>
    </a-dropdown>
  </div>
</template>

<script setup>
import { useStore } from 'vuex'
import { ref, reactive, onMounted } from 'vue'

import { open, save, message } from '@tauri-apps/plugin-dialog'

import { saveFile, buildDirectoryTree, saveAsFile, openFileFn } from '@/utils/markdown.js'

// 键盘事件
import { listen } from '@tauri-apps/api/event'

import { getCurrentWindow } from '@tauri-apps/api/window'

import { message as messageAnt } from 'ant-design-vue'

import { register, unregisterAll, isRegistered } from '@tauri-apps/plugin-global-shortcut'

import { createMarkdownWin } from '@/utils/windows/actions.js'

// // 监听 'shortcut-event' 事件 TODO: 监听快捷键方案一
// listen('shortcut-event', async (event) => {
//   switch (event.payload) {
//     // 打开文件
//     case 'ctrl+alt+o':
//       openFileFn()
//       break
//     // 打开文件夹
//     case 'ctrl+alt+k':
//       openFolder()
//       break
//     // 保存
//     case 'ctrl+s':
//       saveFile()
//       break
//     // 另存为
//     case 'ctrl+alt+s':
//       const saveAsPath = await saveAsFile()
//       buildDirectoryTree(saveAsPath)
//       break
//   }
// })

// 注册快捷键 TODO: 监听快捷键方案二
const GlobalShortcutRegister = async () => {
  // 新建窗口
  if (await isRegistered('CommandOrControl+Alt+Shift+N') === false) {
    await register('CommandOrControl+Alt+Shift+N', (e) => {
      if (e.state == 'Pressed') {
        createMarkdownWin()
      }
    })
  }
  // 打开文件
  if (await isRegistered('CommandOrControl+Alt+O') === false) {
    await register('CommandOrControl+Alt+O', (e) => {
      if (e.state == 'Pressed') {
        openFileFn()
      }
    })
  }
  // 打开文件夹
  if (await isRegistered('CommandOrControl+Alt+K') === false) {
    await register('CommandOrControl+Alt+K', (e) => {
      if (e.state == 'Pressed') {
        openFolder()
      }
    })
  }
  // 保存
  if (await isRegistered('CommandOrControl+S') === false) {
    await register('CommandOrControl+S', (e) => {
      if (e.state == 'Pressed') {
        saveFile()
      }
    })
  }
  // 另存为
  if (await isRegistered('CommandOrControl+Alt+S') === false) {
    await register('CommandOrControl+Alt+S', async (e) => {
      if (e.state == 'Pressed') {
        const saveAsPath = await saveAsFile()
        buildDirectoryTree(saveAsPath)
      }
    })
  }
}
// 注销快捷键
const GlobalShortcutUnregister = async () => {
  await unregisterAll()
}


const init = async () => {
  // 窗口聚焦/失焦事件监听
  try {
    await getCurrentWindow().setFocus()
    const unlistenFocus = await getCurrentWindow().onFocusChanged(({ payload: focused }) => {
      if (focused) {
        GlobalShortcutRegister()
      } else {
        GlobalShortcutUnregister()
      }
    })
  } catch (error) {
    alert(error)
  }
}
init()

const store = useStore()

// 菜单项
const menuOptions = [
  {
    label: '文件',
    key: 'file',
    children: [
      // {
      //   label: '新建[Ctrl+Alt+N]',
      //   key: 'new'
      // },
      // {
      //   label: '新建窗口[Ctrl+ALT+Shift+N]',
      //   key: 'newWindow',
      //   divider: true
      // },
      {
        label: '打开文件[Ctrl+Alt+O]',
        key: 'openFile',
      },
      {
        label: '打开文件夹[Ctrl+Alt+K]',
        key: 'openFolder',
        divider: true
      },
      {
        label: '保存[Ctrl+S]',
        key: 'save',
      },
      {
        label: '另存为[Ctrl+Alt+S]',
        key: 'saveAs',
      }
    ]
  },
  {
    label: '帮助',
    key: 'help',
    children: [
      {
        label: '更新日志',
        key: 'updateLog',
        divider: true
      },
      // {
      //   label: '检查更新',
      //   key: 'checkUpdate',
      // },
      {
        label: '帮助',
        key: 'help',
      }
    ]
  }
]


onMounted(async () => {
})

// 打开文件夹
const openFolder = async () => {
  const openFolderPath = await open({
    multiple: false,
    directory: true
  })
  await buildDirectoryTree(openFolderPath)
  await store.dispatch('SetCurrentFile', {
    path: '',
    content: '',
    type: '',
    status: 'notSaved'
  })
}

const handleClick = async (e) => {
  const { key } = e
  switch (key) {
    // 新建
    case 'new':
      /**
       * TODO: 方案1： 检测当前有未保存的文件，提示是否保存，保存后再打开新文件
       * TODO: 方案2： 直接打开窗口创建一个新的窗口
       */
      break;
    // 新建窗口
    case 'newWindow':
      createMarkdownWin()
      break;
    // 打开文件
    case 'openFile':
      openFileFn()
      break;
    // 打开文件夹
    case 'openFolder':
      openFolder()
      break;
    // 保存
    case'save':
      saveFile()
      break;
    // 另存为
    case'saveAs':
      const saveAsPath = await saveAsFile()
      buildDirectoryTree(saveAsPath)
      break;
  }
}
</script>

<style scoped lang="scss">
.menuWrapper{
  height: 40px;
  line-height: 40px;
  background-color: rgb(251, 251, 251);
  border-bottom: 1px solid #ddd;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  padding: 0 12px;
  font-size: 12px;
  .ant-dropdown-link{
    margin: 0 8px;
  }
}
</style>
